﻿namespace blackhouse.mojoPortalExtend
{
    using blackhouse.Web.Controls;
    using blackhouse.Web.FasterFiles;
    using mojoPortal.Business.WebHelpers;
    using mojoPortal.Web;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Globalization;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ExpandMenu : Control
    {
        private string cssClassLi = "";
        private string cssClassUl = "menu";
        private bool isAdmin = false;
        private bool isContentAdmin = false;
        private int levels = 1;
        private SiteMapDataSource siteMapDataSource;

        private void addCssFile()
        {
            new FasterFilesProvider().AddCssFile(typeof(ExpandMenu), "blackhouse.Web.mojoPortal.superfishmenu.css");
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            SuperfishMenu menu = new SuperfishMenu {
                AutoArrows = false,
                DropShadows = false,
                MenuElementSelector = "." + this.CssClassUl,
                IncludeCss = false
            };
            this.Controls.Add(menu);
        }

        private string formatTitle(string title)
        {
            title = HttpUtility.HtmlDecode(title);
            title = this.MakeAllToLowercase ? CultureInfo.CurrentCulture.TextInfo.ToLower(title) : title;
            title = this.MakeAllToUppercase ? CultureInfo.CurrentCulture.TextInfo.ToUpper(title) : title;
            return title;
        }

        private string getCssClassForLevel(int levels, string classDef, string mojoCss)
        {
            string addToClass = "";
            int forLevel = this.Levels - levels;
            if (forLevel > 0)
            {
                addToClass = "-" + forLevel;
            }
            string[] cssClassParts = classDef.Split(new char[] { ' ' });
            List<string> returnDef = new List<string>(cssClassParts.Length);
            if (!string.IsNullOrEmpty(mojoCss))
            {
                returnDef.Add(mojoCss);
            }
            foreach (string cssClass in cssClassParts)
            {
                if (string.IsNullOrEmpty(addToClass))
                {
                    returnDef.Add(cssClass);
                }
                else
                {
                    returnDef.Add(cssClass + addToClass);
                }
            }
            return string.Join(" ", returnDef.ToArray());
        }

        private void getSiteMapSource()
        {
            this.siteMapDataSource = (SiteMapDataSource) this.Page.Master.FindControl("SiteMapData");
            if (this.siteMapDataSource != null)
            {
                this.siteMapDataSource.SiteMapProvider = "mojosite" + CacheHelper.GetCurrentSiteSettings().SiteId.ToString(CultureInfo.InvariantCulture);
            }
        }

        private bool isHidden(SiteMapNode mapNode, mojoSiteMapNode mojoNode)
        {
            bool remove = false;
            if (((!this.isAdmin && ((!this.isContentAdmin || (mapNode.Roles == null)) || ((mapNode.Roles.Count == 1) || (mapNode.Roles[0].ToString() != "Admins")))) && (!this.isContentAdmin || (mapNode.Roles != null))) && ((mapNode.Roles == null) || !WebUser.IsInRoles(mapNode.Roles)))
            {
                remove = true;
            }
            if (!((mojoNode == null) || mojoNode.IncludeInMenu))
            {
                remove = true;
            }
            if (!(((mojoNode == null) || !mojoNode.IsPending) || WebUser.IsAdminOrContentAdminOrContentPublisherOrContentAuthor))
            {
                remove = true;
            }
            if (((mojoNode != null) && mojoNode.HideAfterLogin) && this.Page.Request.IsAuthenticated)
            {
                remove = true;
            }
            return remove;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (this.UseSuperfishStyle)
            {
                this.addCssFile();
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            this.setSettings();
            this.getSiteMapSource();
            if (this.siteMapDataSource != null)
            {
                this.renderMenuChildrens(writer, this.siteMapDataSource.Provider.RootNode.ChildNodes, this.Levels);
            }
        }

        private void renderMenuChildrens(HtmlTextWriter writer, SiteMapNodeCollection nodes, int levels)
        {
            IEnumerable<SiteMapNode> nodes2 = from smn in nodes.Cast<SiteMapNode>()
                where !this.isHidden(smn, smn as mojoSiteMapNode)
                select smn;
            if ((levels >= 0) && (nodes2.Count<SiteMapNode>() != 0))
            {
                if (!string.IsNullOrEmpty(this.CssClassUl))
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, this.getCssClassForLevel(levels, this.CssClassUl, string.Empty));
                }
                writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                foreach (SiteMapNode node in nodes2)
                {
                    mojoSiteMapNode mojoNode = node as mojoSiteMapNode;
                    if (!this.isHidden(node, mojoNode))
                    {
                        if (!string.IsNullOrEmpty(this.CssClassLi))
                        {
                            writer.AddAttribute(HtmlTextWriterAttribute.Class, this.getCssClassForLevel(levels, this.CssClassLi, (mojoNode == null) ? string.Empty : mojoNode.MenuCssClass));
                        }
                        writer.RenderBeginTag(HtmlTextWriterTag.Li);
                        string url = node.Url;
                        if (url.StartsWith("~/"))
                        {
                            url = url.Substring(1);
                        }
                        writer.AddAttribute("href", url);
                        writer.RenderBeginTag(HtmlTextWriterTag.A);
                        writer.Write(this.formatTitle(node.Title));
                        writer.RenderEndTag();
                        if (levels > 0)
                        {
                            this.renderMenuChildrens(writer, node.ChildNodes, levels - 1);
                        }
                        writer.RenderEndTag();
                    }
                }
                writer.RenderEndTag();
            }
        }

        private void setSettings()
        {
            this.isAdmin = WebUser.IsAdmin;
            this.isContentAdmin = WebUser.IsContentAdmin;
        }

        [DefaultValue("")]
        public string CssClassLi
        {
            get
            {
                return this.cssClassLi;
            }
            set
            {
                this.cssClassLi = value;
            }
        }

        [DefaultValue("menu")]
        public string CssClassUl
        {
            get
            {
                return this.cssClassUl;
            }
            set
            {
                this.cssClassUl = value;
            }
        }

        [DefaultValue(1)]
        public int Levels
        {
            get
            {
                return this.levels;
            }
            set
            {
                this.levels = value;
            }
        }

        [DefaultValue(false)]
        public bool MakeAllToLowercase { get; set; }

        [DefaultValue(false)]
        public bool MakeAllToUppercase { get; set; }

        [DefaultValue(false)]
        public bool UseSuperfishStyle { get; set; }
    }
}

